package com.dragon.loan.service;

import cn.hutool.core.util.StrUtil;
import com.dragon.loan.dto.UserAgentDTO;
import com.dragon.loan.entity.AcUserAgentMapping;
import com.dragon.loan.repository.AcUserAgentMappingMapper;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AcUserAgentMappingServiceImpl implements AcUserAgentMappingService {

	@Autowired
	private AcUserAgentMappingMapper acUserAgentMappingMapper;
	
	@Override
	public UserAgentDTO dealUserAgent(String userAgent) {
		String defaultValue = "未知";
		UserAgentDTO userAgentDTO = new UserAgentDTO();
		userAgentDTO.setBrowser(defaultValue);
		userAgentDTO.setDevice(defaultValue);
		try {
			if(StrUtil.isNotBlank(userAgent)){
				//先分析设备信息
				List<AcUserAgentMapping> deviceMapping = deviceMapping();
				for (AcUserAgentMapping acUserAgentMapping : deviceMapping) {
					if(StrUtil.containsIgnoreCase(userAgent, acUserAgentMapping.getCode())){
						userAgentDTO.setDevice(acUserAgentMapping.getCodeName());
						break;
					}
				}
				
				//分析浏览器
				List<AcUserAgentMapping> browserMapping = browserMapping();
				for (AcUserAgentMapping acUserAgentMapping : browserMapping) {
					if(StrUtil.containsIgnoreCase(userAgent, acUserAgentMapping.getCode())){
						userAgentDTO.setBrowser(acUserAgentMapping.getCodeName());
						break;
					}
				}
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("分析userAgent异常", e);
		}
		// 都没获取到直接返回空
		if(StrUtil.equals(userAgentDTO.getDevice(), defaultValue) && StrUtil.equals(userAgentDTO.getBrowser(), defaultValue)){
			return null;
		}
		return userAgentDTO;
	}
	
	/**
	 * 获取设备映射列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<AcUserAgentMapping> deviceMapping(){
		String key = "deviceMapping_list";
		List<AcUserAgentMapping> deviceMapping = new ArrayList<AcUserAgentMapping>();
		Object mappingObject = CacheUtils.getCacheObject(key);
		if(mappingObject != null){
			return (List<AcUserAgentMapping>) mappingObject;
		}
		deviceMapping = acUserAgentMappingMapper.selectByType("1");
		if(deviceMapping != null && deviceMapping.size() > 0){
			CacheUtils.cacheObject(key, deviceMapping, 15L);// 缓存五分钟
		}
		return deviceMapping;
	}
	
	
	/**
	 * 获取浏览器映射列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private List<AcUserAgentMapping> browserMapping(){
		String key = "browserMapping_list";
		List<AcUserAgentMapping> deviceMapping = new ArrayList<AcUserAgentMapping>();
		Object mappingObject = CacheUtils.getCacheObject(key);
		if(mappingObject != null){
			return (List<AcUserAgentMapping>) mappingObject;
		}
		deviceMapping = acUserAgentMappingMapper.selectByType("2");
		if(deviceMapping != null && deviceMapping.size() > 0){
			CacheUtils.cacheObject(key, deviceMapping, 15L);// 缓存五分钟
		}
		return deviceMapping;
	}
}
